/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RepeatedBlueID.java
 *
 * Created on 25/07/2010, 7:38:57 PM
 */

package utilities;

import java.util.*;
import java.io.*;
import java.util.logging.*;
import java.text.*;


/**
 *
 * @author ATuan
 */
public class RepeatedBlueID extends javax.swing.JDialog {

    private static Logger theLogger =
	 Logger.getLogger(RepeatedBlueID.class.getName());
    private static FileHandler fileTxt;
    private static FileHandler fileXML;

    private static SimpleFormatter formatterTxt;
    public float sAvg=0;
    /** Creates new form RepeatedBlueID */
    public RepeatedBlueID(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
         try{
        formatterTxt = new SimpleFormatter();
        //formatterTxt = new XMLFormatter();
        fileTxt = new FileHandler("RepeatedBlueID.log",true);
        fileTxt.setFormatter(formatterTxt);
        theLogger.addHandler(fileTxt);
        }catch(Exception e){
            theLogger.severe(e.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        file1TF = new javax.swing.JTextField();
        LoadFilesBtn = new javax.swing.JButton();
        file2TF = new javax.swing.JTextField();
        CheckRepeatedBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        sResultTA = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        dateLB = new javax.swing.JLabel();
        timestampLB = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        SPARQLta = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        JessTA = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        OneYearLaterLB = new javax.swing.JLabel();
        NextYearLB = new javax.swing.JLabel();
        CleanUpDataBtn = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        ReverseBtn = new javax.swing.JButton();
        AddTimestampBtn = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        findStringTF = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(placecommframework.PlaceCommFrameworkApp.class).getContext().getResourceMap(RepeatedBlueID.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        file1TF.setText(resourceMap.getString("file1TF.text")); // NOI18N
        file1TF.setName("file1TF"); // NOI18N

        LoadFilesBtn.setText(resourceMap.getString("LoadFilesBtn.text")); // NOI18N
        LoadFilesBtn.setName("LoadFilesBtn"); // NOI18N
        LoadFilesBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LoadFilesBtnMouseClicked(evt);
            }
        });

        file2TF.setText(resourceMap.getString("file2TF.text")); // NOI18N
        file2TF.setName("file2TF"); // NOI18N

        CheckRepeatedBtn.setText(resourceMap.getString("CheckRepeatedBtn.text")); // NOI18N
        CheckRepeatedBtn.setName("CheckRepeatedBtn"); // NOI18N
        CheckRepeatedBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CheckRepeatedBtnMouseClicked(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        sResultTA.setColumns(20);
        sResultTA.setRows(5);
        sResultTA.setName("sResultTA"); // NOI18N
        jScrollPane1.setViewportView(sResultTA);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        dateLB.setText(resourceMap.getString("dateLB.text")); // NOI18N
        dateLB.setName("dateLB"); // NOI18N

        timestampLB.setText(resourceMap.getString("timestampLB.text")); // NOI18N
        timestampLB.setName("timestampLB"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        SPARQLta.setColumns(20);
        SPARQLta.setRows(5);
        SPARQLta.setName("SPARQLta"); // NOI18N
        jScrollPane2.setViewportView(SPARQLta);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        JessTA.setColumns(20);
        JessTA.setRows(5);
        JessTA.setName("JessTA"); // NOI18N
        jScrollPane3.setViewportView(JessTA);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        OneYearLaterLB.setText(resourceMap.getString("OneYearLaterLB.text")); // NOI18N
        OneYearLaterLB.setName("OneYearLaterLB"); // NOI18N

        NextYearLB.setText(resourceMap.getString("NextYearLB.text")); // NOI18N
        NextYearLB.setName("NextYearLB"); // NOI18N

        CleanUpDataBtn.setText(resourceMap.getString("CleanUpDataBtn.text")); // NOI18N
        CleanUpDataBtn.setName("CleanUpDataBtn"); // NOI18N
        CleanUpDataBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CleanUpDataBtnMouseClicked(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        ReverseBtn.setText(resourceMap.getString("ReverseBtn.text")); // NOI18N
        ReverseBtn.setName("ReverseBtn"); // NOI18N
        ReverseBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ReverseBtnMouseClicked(evt);
            }
        });

        AddTimestampBtn.setText(resourceMap.getString("AddTimestampBtn.text")); // NOI18N
        AddTimestampBtn.setName("AddTimestampBtn"); // NOI18N
        AddTimestampBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AddTimestampBtnMouseClicked(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        findStringTF.setText(resourceMap.getString("findStringTF.text")); // NOI18N
        findStringTF.setName("findStringTF"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(file1TF, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(file2TF, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(LoadFilesBtn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CheckRepeatedBtn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                                .addComponent(ReverseBtn))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(CleanUpDataBtn)
                                .addGap(8, 8, 8)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(AddTimestampBtn)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton4))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton5)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NextYearLB)
                            .addComponent(OneYearLaterLB)
                            .addComponent(timestampLB)
                            .addComponent(dateLB))
                        .addContainerGap(53, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(449, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(findStringTF, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)))
                        .addGap(247, 247, 247))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(file1TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dateLB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(file2TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timestampLB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(LoadFilesBtn)
                            .addComponent(CheckRepeatedBtn)
                            .addComponent(ReverseBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(CleanUpDataBtn)
                            .addComponent(jButton3)
                            .addComponent(jButton5))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(AddTimestampBtn)
                            .addComponent(jButton4)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(OneYearLaterLB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NextYearLB)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(findStringTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadFilesBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoadFilesBtnMouseClicked
        // TODO add your handling code here:
        Date d=new Date();
        long l=d.getTime();
        int dd,mm,yy,hh,min, sec;
        dd=d.getDate();
        mm=d.getMonth();
        yy=d.getYear(); // return to offset of 1900. It means: yy=1 --> real y=1901
                        // Result yy=110, means year 2010
        hh=d.getMinutes();
        min=d.getMinutes();
        sec=d.getSeconds();

        dateLB.setText("Date: "+ Integer.toString(dd)+"/"+
                Integer.toString(mm)+"/"+Integer.toString(yy+1900)+", "+
                Integer.toString(hh)+":"+Integer.toString(min)+":"+
                Integer.toString(sec));

        timestampLB.setText("Timestamp: "+l);
        d.setYear(yy+1);
        long l2=d.getTime();
        OneYearLaterLB.setText("One year later: "+l2+" Offset: "+((l2-l)/1000)+" sec");
        NextYearLB.setText("Next year: "+(d.getYear()+1900));

        /*
         *  My simple algorithm
         * Load file 1: put them into the Set1 (not duplicate)
         * Load file 2: put them into the Set2 (not duplicate)
         * File the intersection of Set1 and Set2
         * Display result;
         * 
         */
         //Collections.disjoint()
        
        List list = new LinkedList(); // Add 1 to 10 to list
        for (int i = 1; i <= 10; i++){
        list.add(i);
        //System.out.print(" "+i);
        }
        

        Set set = new HashSet(); // Add 5 random ints to set
        System.out.println("\n");
        System.out.println("First set");
        for (int i = 1; i <= 15; i++){
            //int num= (int) ((Math.random() * 15) + 1);
            int num=i;
        set.add(num);
        System.out.print(" "+num);
        }
        System.out.println("");
 
        System.out.println("Second set");


        Set set2 = new HashSet(); // Add 5 random ints to set
        for (int i = 1; i <= 15; i++){
            int num= (int) ((Math.random() * 15) + 1);
        set2.add(num);

        System.out.print(" "+num);
        }
        System.out.println("");

        set.retainAll(set2);
        Iterator iter2=set.iterator();
        
        //list.retainAll(set); // Remove all data from listthat are not in set
        System.out.println("Result is: ");

        while (iter2.hasNext()){
            System.out.print(iter2.next().toString()+" ");
        }
        
    }//GEN-LAST:event_LoadFilesBtnMouseClicked

    private void CleanUpDataBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CleanUpDataBtnMouseClicked
        // TODO add your handling code here:
    try{
        String sFilename1=file1TF.getText().trim();
        String sFilename2=file2TF.getText().trim();


        File outputFile = new File(sFilename2);
        PrintWriter pw = new PrintWriter(new FileWriter(outputFile));
      

        FileInputStream fstream = new FileInputStream(sFilename1);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        //Read File Line By Line
        long lDuration=0, lCount=0;
        Date d1=new Date();
        long lT1=d1.getTime();
        String stemp="\n";
        String dbgString="";
        while ((line = br.readLine()) != null) {
           if (!line.trim().contains("Date:")) {
                try{
               String [] sVal=line.split(",");
               dbgString =sVal[0];
               int iVal=sVal[0].length();
               int splitsize=sVal.length;

               long timestamp=Long.parseLong(sVal[0].substring(2,iVal));

               Date d=new Date();
               d.setTime(timestamp);
               line=line+",DdD:"+d.toString();

               }catch (Exception e2){

                  theLogger.severe("StringxYz: "+dbgString+", error converting timestamp: "+e2.toString());
               }

               pw.println(line);
               pw.flush();
           }else {
              System.out.println("Skip line: "+line);
           }
      }
      pw.close();
      br.close();
      System.out.println("File Processed ! Done");
    }catch (Exception ex1){
              theLogger.severe("Error "+ex1.toString());
    }

    }//GEN-LAST:event_CleanUpDataBtnMouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        // TODO add your handling code here:
        String line="T:1225359411640,001FCC5E0FB2,SPH-W3300(37**),D:194";

        try{
               String [] sVal=line.split(",");
               int iVal=sVal[0].length();
               int splitsize=sVal.length;

               long timestamp=Long.parseLong(sVal[0].substring(2,iVal));
               System.out.println("timestamp="+timestamp);
               Date d=new Date();
               d.setTime(timestamp);
               line=line+",date:"+d.toString();
               System.out.println("line:"+line);
               }catch (Exception e2){
                 theLogger.severe("Error converting timestamp: "+e2.toString());
               }
    }//GEN-LAST:event_jButton3MouseClicked

    private void CheckRepeatedBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CheckRepeatedBtnMouseClicked
        // TODO add your handling code here:
   try{
        String sFilename1=file1TF.getText().trim();
        String sFilename2=file2TF.getText().trim();

        FileInputStream fstream1 = new FileInputStream(sFilename1);
        FileInputStream fstream2 = new FileInputStream(sFilename2);
        // Get the object of DataInputStream
        DataInputStream in1 = new DataInputStream(fstream1);
            BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));

        DataInputStream in2 = new DataInputStream(fstream2);
            BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));

        String line;


        Date d=new Date();
        long l=d.getTime();
        int dd,mm,yy,hh,min, sec;
        dd=d.getDate();
        mm=d.getMonth();
        yy=d.getYear(); // return to offset of 1900. It means: yy=1 --> real y=1901
                        // Result yy=110, means year 2010
        hh=d.getMinutes();
        min=d.getMinutes();
        sec=d.getSeconds();

        dateLB.setText("Date: "+ Integer.toString(dd)+"/"+
                Integer.toString(mm)+"/"+Integer.toString(yy+1900)+", "+
                Integer.toString(hh)+":"+Integer.toString(min)+":"+
                Integer.toString(sec));

        timestampLB.setText("Timestamp: "+l);
        d.setYear(yy+1);
        long l2=d.getTime();
        OneYearLaterLB.setText("One year later: "+l2+" Offset: "+((l2-l)/1000)+" sec");
        NextYearLB.setText("Next year: "+(d.getYear()+1900));

        /*
         *  My simple algorithm
         * Load file 1: put them into the Set1 (not duplicate)
         * Load file 2: put them into the Set2 (not duplicate)
         * File the intersection of Set1 and Set2
         * Display result;
         *
         */
         //Collections.disjoint()

        Set set1 = new HashSet();
        Set set2 = new HashSet(); 

        // Add instance from file1 to set 1 
        while ((line = br1.readLine()) != null) {
            try{
            String [] sVal=line.split(",");               
               set1.add(sVal[1]);
            }catch (Exception e2){
                  theLogger.severe("error adding MAC to set: "+e2.toString());
            }               
        }
        System.out.println("Add instance from file1 to set 1! Done!");
      br1.close();        
        // Add instance from file2 to set2
      while ((line = br2.readLine()) != null) {
          try{
               String [] sVal=line.split(",");               
               set2.add(sVal[1]);
          }catch (Exception e2){
                  theLogger.severe("error adding MAC to set: "+e2.toString());
               }                          
      }      
      System.out.println("Add instance from file1 to set 2! Done!");
      br2.close();
      
      set1.retainAll(set2);
      Iterator iter2=set1.iterator();

      System.out.println("IDs found from file 1 in file 2: ");
      while (iter2.hasNext()){
            System.out.print(iter2.next().toString()+" ");
      }

       }catch(Exception e){
            theLogger.severe("File not found:"+e.toString());
       }

    }//GEN-LAST:event_CheckRepeatedBtnMouseClicked

    private void ReverseBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ReverseBtnMouseClicked
        // TODO add your handling code here:
        String s1,s2,s3;
        s1=file1TF.getText().trim();
        s2=file2TF.getText().trim();
        s3=s1;
        s1=s2;
        s2=s3;

        file1TF.setText(s1);
        file2TF.setText(s2);
        
    }//GEN-LAST:event_ReverseBtnMouseClicked

    private void AddTimestampBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddTimestampBtnMouseClicked

       try{
        String sSourceFile=file1TF.getText().trim();
        String sDestFile=file2TF.getText().trim();


        File outputFile = new File(sDestFile);
        PrintWriter pw = new PrintWriter(new FileWriter(outputFile));

        FileInputStream fstream = new FileInputStream(sSourceFile);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line,stemp="";
        //Read File Line By Line

        Date dTimestamp=null;
        long lTimestamp=0;
        while ((line = br.readLine()) != null) {
           if (line.trim().contains("Date:")) {
              dTimestamp=DateConversion(6,line);
              stemp=dTimestamp.toString();
              lTimestamp=dTimestamp.getTime();

              System.out.println("Skip line: "+line);
           }else if (!line.trim().contains("Date:")) {
                try{               
               line="T:"+lTimestamp+","+line+",DdD:"+stemp;
               }catch (Exception e2){
                  theLogger.severe("Error string -> timestamp: "+e2.toString());
                  //break;
               }
               pw.println(line);
               pw.flush();
           }
      }
      pw.close();
      br.close();
      System.out.println("File Processed ! Done");
    }catch (Exception ex1){
              theLogger.severe("Error "+ex1.toString());
    }

    }//GEN-LAST:event_AddTimestampBtnMouseClicked

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
  try{
        String sSourceFile=file1TF.getText().trim();
        String sDestFile=file2TF.getText().trim();


        File outputFile = new File(sDestFile);
        PrintWriter pw = new PrintWriter(new FileWriter(outputFile));

        FileInputStream fstream = new FileInputStream(sSourceFile);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line,stemp="";
        //Read File Line By Line

        Date dTimestamp=null;
        long lTimestamp=0;
        while ((line = br.readLine().trim()) != null) {
           if (line.contains(findStringTF.getText().trim())) {
               int iLoc=line.indexOf("DdD:");
              dTimestamp=DateConversion(iLoc+4,line);
              stemp=dTimestamp.toString();
              lTimestamp=dTimestamp.getTime();
              line="T:"+lTimestamp+","+line;
              pw.println(line);
              pw.flush();
           }
      }
      pw.close();
      br.close();
      System.out.println("File Processed ! Done");
    }catch (Exception ex1){
              theLogger.severe("Error "+ex1.toString());
    }
    }//GEN-LAST:event_jButton4MouseClicked

    private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked
        // TODO add your handling code here:
          try{
        String sSourceFile=file1TF.getText().trim();
        String sDestFile=file2TF.getText().trim();


        File outputFile = new File(sDestFile);
        PrintWriter pw = new PrintWriter(new FileWriter(outputFile));

        FileInputStream fstream = new FileInputStream(sSourceFile);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line,stemp="";
        //Read File Line By Line

        Date dTimestamp=null;
        long lTimestamp=0;
        while ((line = br.readLine()) != null) {
           if (line.trim().contains(findStringTF.getText().trim())) {
               int iLoc=line.indexOf("DdD:");
              dTimestamp=DateConversion(iLoc+4,line);
              stemp=dTimestamp.toString();
              lTimestamp=dTimestamp.getTime();
              line="T:"+lTimestamp+","+line;
              pw.println(line);
              pw.flush();
           }
      }
      pw.close();
      br.close();
      System.out.println("File Processed ! Done");
    }catch (Exception ex1){
              theLogger.severe("Error "+ex1.toString());
    }
    }//GEN-LAST:event_jButton5MouseClicked
 public Date DateConversion(int iLoc, String sInput){
     Date today=null;
     //String stimestamp="Date: Tue Sep 09 12:09:18 GMT+10:00 2008";
     String stimestamp=sInput.substring(iLoc,sInput.length()); // Omit "Date:"
     DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy");
        try{
            today = df.parse(stimestamp);
            System.out.println("Today = " + df.format(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
   return today;     
 }
    public Set Intersection(Set set1, Set set2){
    Set resultSet=new HashSet();

    
    return resultSet;
    }
    public void getTimestampToLong(){
        

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RepeatedBlueID dialog = new RepeatedBlueID(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddTimestampBtn;
    private javax.swing.JButton CheckRepeatedBtn;
    private javax.swing.JButton CleanUpDataBtn;
    private javax.swing.JTextArea JessTA;
    private javax.swing.JButton LoadFilesBtn;
    private javax.swing.JLabel NextYearLB;
    private javax.swing.JLabel OneYearLaterLB;
    private javax.swing.JButton ReverseBtn;
    private javax.swing.JTextArea SPARQLta;
    private javax.swing.JLabel dateLB;
    private javax.swing.JTextField file1TF;
    private javax.swing.JTextField file2TF;
    private javax.swing.JTextField findStringTF;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea sResultTA;
    private javax.swing.JLabel timestampLB;
    // End of variables declaration//GEN-END:variables

}
